package com.nuoniu.sibanyun.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.project.DevProject;
import com.nuoniu.sibanyun.entity.project.SysRdtField;
import com.nuoniu.sibanyun.entity.project.SysRdtForm;
import com.nuoniu.sibanyun.mapper.project.SysRdtFormMapper;
import com.nuoniu.sibanyun.service.project.IDevProjectService;
import com.nuoniu.sibanyun.service.project.ISysRdtFieldService;
import com.nuoniu.sibanyun.service.project.ISysRdtFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 表单明细 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-06-05
 */
@Service
public class SysRdtFormServiceImpl extends ServiceImpl<SysRdtFormMapper, SysRdtForm> implements ISysRdtFormService {

    @Autowired
    private ISysRdtFieldService rdtFieldService;
    @Autowired
    private IDevProjectService devProjectService;


    @Override
    @Transactional
    public void importData(Integer projectId) {
        if (null == projectId) {
            throw new NuoNiuException("项目ID不能为空");
        }
        DevProject devProject = devProjectService.getById(projectId);
        if (null == devProject) {
            throw new NuoNiuException("项目不存在");
        }
        QueryWrapper<SysRdtForm> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id",projectId);
        List<SysRdtForm> formList = this.list(wrapper);
        if(formList.size() > 0) {
            throw new NuoNiuException("已有数据,不能导入");
        }

        //导入表数据
        //1. 从数据库 拉数据
        List<SysRdtForm> sysRdtForms = baseMapper.getFromList();
        if(sysRdtForms.size() > 0) {
            sysRdtForms.forEach(f-> f.setProjectId(projectId));
            saveBatch(sysRdtForms);
            //2.导入字段数据
            for(SysRdtForm sysRdtForm : sysRdtForms) {
                List<SysRdtField> sysRdtFields = rdtFieldService.getByForm(sysRdtForm.getFormNameEn());
                sysRdtFields.forEach(f -> {
                    f.setFormId(sysRdtForm.getId().intValue());
                    f.setProjectId(projectId);
                });
                rdtFieldService.saveBatch(sysRdtFields);
            }
        }
    }


    @Override
    public List<SysRdtForm> getListByProjectId(Integer projectId,String keyWord) {
        QueryWrapper<SysRdtForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectId);
        if(StringUtils.isNotBlank(keyWord)){
            queryWrapper.and(wrapper -> wrapper.like("form_name_en",keyWord).or().like("form_name_cn", keyWord));
        }
        return list( queryWrapper );
    }

    @Override
    public List<SysRdtForm> getList(Integer menuId,Integer projectId) {
        if(null == projectId ) {
            throw new NuoNiuException("请选择项目");
        }
        if(null == menuId ) {
            throw new NuoNiuException("请选择菜单");
        }
        List<SysRdtForm> list = baseMapper.getList(menuId,projectId);
        return list;
    }


    @Override
    public List<SysRdtField> getFieldList(Integer formId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("form_id", formId);
        wrapper.orderByAsc("sort");
        return rdtFieldService.list(wrapper);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteById(Long id, String menuId) {
        if(null == id) {
            throw new NuoNiuException("请选择表");
        }
        if(null == menuId) {
            throw new NuoNiuException("请选择菜单");
        }
        SysRdtForm sysRdtForm = getById(id);
        if(null == sysRdtForm) {
            throw new NuoNiuException("表不存在");
        }
        String menuIds = sysRdtForm.getMenuIds();
        if(StringUtils.isBlank(menuIds)) {
            throw new NuoNiuException("该表和菜单已经没有关联");
        }
        List<String> menuIdList = new ArrayList<>(Arrays.asList(menuIds.split(",")));
        if(!menuIdList.contains(menuId)) {
            throw new NuoNiuException("该表和菜单已经没有关联");
        }
        menuIdList.remove(menuId);
        String newMenuIds = StringUtils.join(menuIdList, ",");
        if(StringUtils.isBlank(newMenuIds)){
            //直接删除
            removeById(id);
            QueryWrapper<SysRdtField> sysRdtFieldQueryWrapper = new QueryWrapper<>();
            sysRdtFieldQueryWrapper.eq("form_id",id);
            rdtFieldService.remove(sysRdtFieldQueryWrapper);
        }else{
            sysRdtForm.setMenuIds(newMenuIds);
            SysRdtForm newSysRdtForm = new SysRdtForm();
            newSysRdtForm.setId(sysRdtForm.getId());
            newSysRdtForm.setMenuIds(newMenuIds);
            updateById(newSysRdtForm);
        }
    }

    @Override
    public void saveField(List<SysRdtField> fields) {
        rdtFieldService.saveOrUpdateBatch(fields);
    }

    @Override
    public void deleteField(Long id) {
        rdtFieldService.removeById(id);
    }
}
